import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

regions = ["华南", "华北", "东北", "西北", "华东"]
completion_2022 = [86, 74, 62, 51, 35]
completion_2021 = [92, 69, 53, 39, 45]

fig, ax = plt.subplots(figsize=(10, 6))
ax.set_title("2022年上半年销量目标达成率与2021年对比")
ax.set_xlabel("完成率 (%)")
ax.set_yticks(range(len(regions)))
ax.set_yticklabels(regions)

ax.invert_yaxis()

bar_height = 0.2
for i in range(len(regions)):

    ax.barh(i, 100, height=bar_height, color='lightgreen', alpha=0.6)

    ax.barh(i, completion_2022[i], height=bar_height, color='skyblue', alpha=0.7)

ax.scatter(completion_2021, range(len(regions)), s=400, color='red', alpha=0.9, label="2021完成率")
ax.scatter(completion_2022, range(len(regions)), s=400, color='blue', alpha=0.9, label="2022完成率")

for i, comp_2022 in enumerate(completion_2022):
    ax.text(comp_2022, i + 0.3, f"{comp_2022}%", va='bottom', ha='center', color='skyblue', fontweight='bold')

for i, comp_2021 in enumerate(completion_2021):
    ax.text(comp_2021, i + 0.3, f"{comp_2021}%", va='bottom', ha='center', color='skyblue', fontweight='bold')

ax.legend()
plt.grid(axis='x', linestyle='--', alpha=0.6)

#plt.savefig('D:/python charm/01/可视化/第二章/图片/对比滑珠图.png')

plt.show()